package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.QuickReply;
import com.ruoyi.system.service.IQuickReplyService;

/**
 * 客服快捷回复控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/api/chat/quick-reply")
public class QuickReplyController extends BaseController
{
    @Autowired
    private IQuickReplyService quickReplyService;

    /**
     * 获取快捷回复列表
     */
    @GetMapping("/list")
    public AjaxResult list()
    {
        LoginUser loginUser = getLoginUser();
        String serviceId = loginUser.getUserId().toString();
        
        List<QuickReply> list = quickReplyService.selectQuickReplyByServiceId(serviceId);
        return AjaxResult.success(list);
    }
    
    /**
     * 获取分类的快捷回复列表
     */
    @GetMapping("/category/{category}")
    public AjaxResult getByCategory(@PathVariable("category") String category)
    {
        LoginUser loginUser = getLoginUser();
        String serviceId = loginUser.getUserId().toString();
        
        List<QuickReply> list = quickReplyService.selectQuickReplyByCategory(serviceId, category);
        return AjaxResult.success(list);
    }
    
    /**
     * 获取单个快捷回复
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") Long id)
    {
        QuickReply quickReply = quickReplyService.selectQuickReplyById(id);
        return AjaxResult.success(quickReply);
    }
    
    /**
     * 新增快捷回复
     */
    @PostMapping
    public AjaxResult add(@RequestBody QuickReply quickReply)
    {
        LoginUser loginUser = getLoginUser();
        quickReply.setServiceId(loginUser.getUserId().toString());
        
        quickReplyService.insertQuickReply(quickReply);
        return AjaxResult.success();
    }
    
    /**
     * 修改快捷回复
     */
    @PutMapping
    public AjaxResult update(@RequestBody QuickReply quickReply)
    {
        LoginUser loginUser = getLoginUser();
        String serviceId = loginUser.getUserId().toString();
        
        // 检查是否是当前客服的快捷回复
        QuickReply existReply = quickReplyService.selectQuickReplyById(quickReply.getId());
        if (existReply == null || !existReply.getServiceId().equals(serviceId))
        {
            return AjaxResult.error("只能修改自己的快捷回复");
        }
        
        quickReplyService.updateQuickReply(quickReply);
        return AjaxResult.success();
    }
    
    /**
     * 删除快捷回复
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        LoginUser loginUser = getLoginUser();
        String serviceId = loginUser.getUserId().toString();
        
        // 检查是否是当前客服的快捷回复
        QuickReply existReply = quickReplyService.selectQuickReplyById(id);
        if (existReply == null || !existReply.getServiceId().equals(serviceId))
        {
            return AjaxResult.error("只能删除自己的快捷回复");
        }
        
        quickReplyService.deleteQuickReplyById(id);
        return AjaxResult.success();
    }
    
    /**
     * 获取快捷回复分类列表
     */
    @GetMapping("/categories")
    public AjaxResult getCategories()
    {
        // 这里可以从数据库中获取所有分类，为简化实现，直接返回固定分类
        String[] categories = {"问候", "结束语", "服务指南", "服务范围", "服务流程", "服务评价", "等待", "优惠券"};
        return AjaxResult.success(categories);
    }
} 